/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin.exam;

import hibernate.entity.Courses;
import hibernate.entity.ExamResult;
import hibernate.entity.ExamResultId;
import hibernate.entity.Exams;
import hibernate.entity.Students;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.HttpSession;
import ui.admin.AbstractAdminBean;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class ExamResultManager extends AbstractAdminBean {

    /**
     * Creates a new instance of ExamResult
     */
    List<ExamResult> listExamResult = new ArrayList<ExamResult>();
    Exams currExam;

    public List<ExamResult> getListExamResult() {
        return listExamResult;
    }

    public void setListExamResult(List<ExamResult> listExamResult) {
        this.listExamResult = listExamResult;
    }

    public Exams getCurrExam() {
        return currExam;
    }

    public void setCurrExam(Exams currExam) {
        this.currExam = currExam;
    }

    public ExamResultManager() {
        HttpSession session = getHttpSession();
        currExam = (Exams) session.getAttribute("currExam");
        checkExamResult(currExam);
        listExamResult = Converter.iteratorToList(currExam.getExamResults().iterator());
    }

    public String updateAll() {
        hbTransaction = hbSession.beginTransaction();
        try {
            for (ExamResult examResult : listExamResult) {
                examResult = (ExamResult) hbSession.merge(examResult);
                hbSession.update(examResult);
            }
            hbTransaction.commit();
            return "editresult";
        } catch (Exception e) {
            hbTransaction.rollback();
        }
        return null;
    }

    void checkExamResult(Exams ex) {
        Courses courses = ex.getCourses();
        List<Students> listS = Converter.iteratorToList(courses.getStudentses().iterator());
        List<ExamResult> resultsOfExam = Converter.iteratorToList(ex.getExamResults().iterator());
        try {
            hbTransaction = hbSession.beginTransaction();
            for (Students students : listS) {
                boolean exist = false;
                for (ExamResult examResult : resultsOfExam) {
                    if (examResult.getStudents().getStudentId() == students.getStudentId()) {
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    ExamResult er = new ExamResult(ex, students);
                    ExamResultId exID = new ExamResultId(ex.getExamId(), students.getStudentId());
                    er.setId(exID);
                    hbSession.save(er);
                }
            }
            hbTransaction.commit();
        } catch (Exception e) {
            hbTransaction.rollback();
        }

    }
}